﻿using Photocopy.Business;
using Photocopy.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Photocopy
{
    public partial class Product : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindRepeater();
            }

        }
        private void bindRepeater()
        {
            ProductData product = new ProductData();
            CollectionPager1.MaxPages = 10000;
            CollectionPager1.PageSize = 12;
            CollectionPager1.DataSource = product.getProduct();
            CollectionPager1.BindToControl = Repeater1;
            Repeater1.DataSource = CollectionPager1.DataSourcePaged;
            Repeater1.DataBind();
                         
        }
        private void SortbyID()
        {
            ProductData product = new ProductData();
            CollectionPager1.MaxPages = 10000;
            CollectionPager1.PageSize = 12;
            CollectionPager1.DataSource = product.getProduct();
            CollectionPager1.BindToControl = Repeater1;
            Repeater1.DataSource = CollectionPager1.DataSourcePaged;
            Repeater1.DataBind();
        }
        private void SortbyName()
        {
            ProductData product = new ProductData();
            CollectionPager1.MaxPages = 10000;
            CollectionPager1.PageSize = 12;
            CollectionPager1.DataSource = product.getProduct().OrderBy(item => item.TenSanPham).ToList();
            CollectionPager1.BindToControl = Repeater1;
            Repeater1.DataSource = CollectionPager1.DataSourcePaged;
            Repeater1.DataBind();
        }
        private void SortbyCompany()
        {
            ProductData product = new ProductData();
            CollectionPager1.MaxPages = 10000;
            CollectionPager1.PageSize = 12;
            CollectionPager1.DataSource = product.getProduct().OrderBy(item => item.NhaSanXuat).ToList();
            CollectionPager1.BindToControl = Repeater1;
            Repeater1.DataSource = CollectionPager1.DataSourcePaged;
            Repeater1.DataBind();
        }
        private void SortbyPrice()
        {
            ProductData product = new ProductData();
            CollectionPager1.MaxPages = 10000;
            CollectionPager1.PageSize = 12;
            CollectionPager1.DataSource = product.getProduct().OrderBy(item => item.DonGia).ToList();
            CollectionPager1.BindToControl = Repeater1;
            Repeater1.DataSource = CollectionPager1.DataSourcePaged;
            Repeater1.DataBind();
        }
        

        //protected void GetDetail(object sender, EventArgs e)
        //{        
        //    ProductData product = new ProductData();
        //    Session["Masanpham"] = product.getProduct("MaSanPham");
        //    Response.Redirect("ProductDetails.aspx");
        //}

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch(DropDownList1.SelectedIndex)
            {
                case 0:
                    SortbyID();
                    break;
                case 1:
                    SortbyName();
                    break;
                case 2:
                    SortbyCompany();
                    break;
                case 3:
                    SortbyPrice();
                    break;
            }
            
                
        }

        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "GetProductDetail1")
            {
                Label lblsanpham = (Label)e.Item.FindControl("lbSanphamID1");
                int SanphamID = Int32.Parse(lblsanpham.Text);
                ProductData product = new ProductData();
                Session["sanpham"] = product.getProduct(SanphamID);
                Response.Redirect("ProductDetails.aspx");
            }
        }


    }
}